import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/student',
    name: 'StudentLayout',
    component: () => import('../layouts/StudentLayout.vue'),
    meta: { requiresAuth: true, role: 'stu' },
    children: [
      {
        path: 'dashboard',
        name: 'StudentDashboard',
        component: () => import('../views/student/Dashboard.vue')
      },
      {
        path: 'announcements',
        name: 'StudentAnnouncements',
        component: () => import('../views/student/Announcements.vue')
      },
      {
        path: 'select-room',
        name: 'SelectRoom',
        component: () => import('../views/student/SelectRoom.vue')
      },
      {
        path: 'profile',
        name: 'StudentProfile',
        component: () => import('../views/student/Profile.vue')
      },
      {
        path: 'payment',
        name: 'Payment',
        component: () => import('../views/student/Payment.vue')
      },
      {
        path: 'questionnaire',
        name: 'Questionnaire',
        component: () => import('../views/student/Questionnaire.vue')
      },
      {
        path: 'repair',
        name: 'Repair',
        component: () => import('../views/student/Repair.vue')
      }
    ]
  },
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('../layouts/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/Dashboard.vue')
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('../views/admin/UserManagement.vue')
      },
      {
        path: 'dormitories',
        name: 'DormitoryManagement',
        component: () => import('../views/admin/DormitoryManagement.vue')
      },
      {
        path: 'rules',
        name: 'RuleManagement',
        component: () => import('../views/admin/RuleManagement.vue')
      },
      {
        path: 'announcements',
        name: 'AnnouncementManagement',
        component: () => import('../views/admin/AnnouncementManagement.vue')
      },
      {
        path: 'orders',
        name: 'OrderManagement',
        component: () => import('../views/admin/OrderManagement.vue')
      },
      {
        path: 'questionnaire-reports',
        name: 'QuestionnaireReports',
        component: () => import('../views/admin/QuestionnaireReports.vue')
      },
      {
        path: 'repair-management',
        name: 'RepairManagement',
        component: () => import('../views/admin/RepairManagement.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresAuth) {
    if (!userStore.isLoggedIn) {
      next('/login')
      return
    }
    
    if (to.meta.role && userStore.userInfo.role !== to.meta.role) {
      next('/login')
      return
    }
    
    // 如果是学生且不是去问卷页面，检查是否已填写问卷
    if (userStore.userInfo.role === 'stu' && 
        !to.path.includes('/student/questionnaire') && 
        to.path.startsWith('/student')) {
      try {
        const response = await fetch('/api/questionnaire/check-status', {
          method: 'GET',
          headers: {
            'satoken': userStore.token
          }
        })
        const result = await response.json()
        
        if (result.code === 200 && !result.data) {
          // 未填写问卷，跳转到问卷页面
          next('/student/questionnaire')
          return
        }
      } catch (error) {
        console.error('检查问卷状态失败:', error)
      }
    }
  }
  
  next()
})

export default router 